package com.cskaoyan.datasource;

import com.cskaoyan.jdbc.util.JDBCUtils;
import com.mysql.jdbc.JDBC4Connection;

import java.sql.Connection;
import java.util.LinkedList;

/**
 * 创建日期: 2022/06/05 11:06
 *
 * @author ciggar
 */
public class MyConnectionPoolV1 {

    // 声明一个存放连接的容器  从尾部拿，从头部存
    static LinkedList<Connection> linkedList;

    // 阿里巴巴开发手册
    // 王道编程规范




    static {
        // 初始化
        linkedList = new LinkedList<Connection>();

        for (int i = 0; i < 10; i++) {

            Connection connection = JDBCUtils.getConnection();
            linkedList.addFirst(connection);

        }


    }


    // 获取连接
    public static Connection getConnection(){

        Connection connection = linkedList.removeLast();

        return connection;


    }




    // 返回连接
    public static void returnConnection(Connection connection){
        linkedList.addFirst(connection);

    }
}
